/*************************************************************************
 *
 *  Copyright (C) SEIKO EPSON CORP. 1997
 *  All Rights Reserved
 *
 *  Filename : itron.h
 *  Function : 
 *  Revision :
 *          1997/08/01  H.Matsuoka  start
 *          1999/08/26  H.Matsuoka  Add message buffer function
 *          2000/11/21  Y.Taka      Add memory pool management function
 *          2001/01/16  Y.Taka      Add tslp_tsk,twai_sem,twai_flg,trcv_msg
 *
 ************************************************************************/
#ifndef _ITRON_
#define _ITRON_

/***  General-Purpose Data Types *******************************************/
typedef char            B;      /* signed 8-bit integer */
typedef short           H;      /* signed 16-bit integer */
typedef long            W;      /* signed 32-bit integer */
typedef unsigned char   UB;     /* unsigned 8-bit integer */
typedef unsigned short  UH;     /* unsigned 16-bit integer */
typedef unsigned long   UW;     /* unsigned 32-bit integer */

typedef long            VW;     /* unpredictable data type (32-bit size) */
typedef short           VH;     /* unpredictable data type (16-bit size) */
typedef char            VB;     /* unpredictable data type (8-bit size) */

typedef void            *VP;    /* pointer to an unpredictable data type */
typedef void            (*FP)();/* program start address */

/***  Data Types Dependent on ITRON Specification **************************/
typedef int             INT;    /* Signed integer (bit width of processor) */
typedef unsigned int    UINT;   /* Unsigned integer (bit width of processor) */
typedef H               BOOL;   /* Boolean value.  TRUE (1) or FALSE (0). */
typedef short           FN;     /* Function code.  Signed integer.  
                                   Maximum 2 bytes. */
typedef INT             ID;     /* Object ID number (???id)
                                   Value range depends on the system.  
                                   Usually a signed integer. */
typedef INT             BOOL_ID;/* Boolean value or ID number */
typedef INT             HNO;    /* Handler number */
typedef INT             RNO;    /* Rendezvous number */
typedef H               NODE;   /* Node Number.  Usually a signed integer. */
typedef UINT            ATR;    /* Object or handler attribute.  
                                   An unsigned integer. */
typedef INT             ER;     /* Error code.  A signed integer. */
typedef INT             PRI;    /* Task priority.  A signed integer. */
typedef INT             TMO;    /* Timeout value.  A signed integer. */

typedef TMO             DLYTIME;

/***************************************************************************
***    6.6 Common Constants and Data Structure Packet Formats            ***
***************************************************************************/

/* --- overall ----------------------- */

#define NADR        (-1)    /* invalid address or pointer value */
#define TRUE        1       /* true */
#define FALSE       0       /* false */

#define TA_ASM      0x0     /* program written in assembly language */
#define TA_HLNG     0x1     /* program written in high-level language */

#define TMO_POL     0       /* polling */
#define TMO_FEVR    (-1)    /* wait forever */

/* --- for task management functions ----------------------- */

#define TSK_SELF    0       /* task specifies itself */
#define TPRI_INI    0       /* specifies the initial priority on task startup
                               (chg_pri) */
#define TPRI_RUN    0       /* specifies the highest priority during execution
                               (rot_rdq) */

#define TTS_RUN     0x00000001  /* RUN */
#define TTS_RDY     0x00000002  /* READY */
#define TTS_WAI     0x00000004  /* WAIT */
#define TTS_SUS     0x00000008  /* SUSPEND */
#define TTS_WAS     0x0000000c  /* WAIT-SUSPEND */
#define TTS_DMT     0x00000010  /* DORMANT */

#define TTW_SLP     0x00000001  /* wait due to slp_tsk or tslp_tsk */
#define TTW_DLY     0x00000002  /* wait due to dly_tsk */
#define TTW_NOD     0x00000008  /* connection function response wait */
#define TTW_FLG     0x00000010  /* wait due to wai_flg or twai_flg */
#define TTW_SEM     0x00000020  /* wait due to wai_sem or twai_sem */
#define TTW_MBX     0x00000040  /* wait due to rcv_msg or trcv_msg */
#define TTW_SMBF    0x00000080  /* wait due to snd_mbf, tsnd_mbf */
#define TTW_MBF     0x00000100  /* wait due to rcv_mbf, trcv_mbf */
#define TTW_MPL     0x00000200  /* wait due to get_blk, tget_blk */
#define TTW_MPF     0x00000400  /* wait due to get_blf, tget_blf */

/* --- for eventflag functions ----------------------- */

#define TA_WSGL     0x00000000  /* multiple tasks are not allowed to wait (Wait
                                   Single Task) */
#define TA_WMUL     0x00000008  /* multiple tasks are allowed to wait (Wait
                                   Multiple Task) */

#define TWF_ANDW    0x00000000  /* AND wait */
#define TWF_ORW     0x00000002  /* OR wait */
#define TWF_CLR     0x00000001  /* clear specification */

/* --- for mailbox functions ----------------------- */

#define TA_MFIFO    0x00000000  /* waiting tasks are handled by FIFO */
#define TA_MPRI     0x00000002  /* waiting tasks are handled by priority */

typedef struct t_msg {
    struct t_msg*   pNxt;
    VB              msgcont[10];
} T_MSG;

/* --- for time management functions ----------------------- */

/* example for 32-bit CPUs */
typedef struct t_systime {
    H    utime;     /* upper 16 bits */
    UW   ltime;     /* lower 32 bits */
} SYSTIME;

#define TCY_OFF     0x00000000  /* cyclic handler off */
#define TCY_ON      0x00000001  /* cyclic handler on */
#define TCY_INI     0x00000002  /* cycle timer init */

#define TTM_ABS     0x00000000  /* absolute alarm time */
#define TTM_REL     0x00000001  /* relative alarm time */

#define HNDR_CYC    0x00000001  /* wait due to cyclic handler */
#define HNDR_ALM    0x00000002  /* wait due to alarm handler */

/* --- for system management functions ----------------------- */

/* get_ver */
typedef struct t_ver {
    UH   maker;     /* vendor */
    UH   id;        /* format number */
    UH   spver;     /* specification version */
    UH   prver;     /* product version */
    UH   prno[4];   /* product control information */
    UH   cpu;       /* CPU information */
    UH   var;       /* variation descriptor */
} T_VER;

#define TSS_TSK    0    /* normal state in which dispatching is enabled during
                           task portion execution */
#define TSS_DDSP   1    /* state after dis_dsp has been executed during task
                           portion execution (dispatch disabled) */
#define TSS_LOC    3    /* state after loc_cpu has been executed during task
                           portion execution(interrupt and dispatch disabled)*/
#define TSS_INDP   4    /* state during execution of task-independent portions
                           (interrupt and timer handlers) */



/****************************************************************************/
/***    6.5 C Language Interfaces                                         ***/
/****************************************************************************/

/* Task Management Functions */

#define sta_tsk (*tpDL_ROS33Table->ros33_sta_tsk)
#define ext_tsk (*tpDL_ROS33Table->ros33_ext_tsk)
#define ter_tsk (*tpDL_ROS33Table->ros33_ter_tsk)
#define dis_dsp (*tpDL_ROS33Table->ros33_dis_dsp)
#define ena_dsp (*tpDL_ROS33Table->ros33_ena_dsp)
#define chg_pri (*tpDL_ROS33Table->ros33_chg_pri)
#define rot_rdq (*tpDL_ROS33Table->ros33_rot_rdq)
#define rel_wai (*tpDL_ROS33Table->ros33_rel_wai)
#define get_tid (*tpDL_ROS33Table->ros33_get_tid)

/* Task-Dependent Synchronization Functions */

#define sus_tsk	(*tpDL_ROS33Table->ros33_sus_tsk)
#define rsm_tsk (*tpDL_ROS33Table->ros33_rsm_tsk)

#define tslp_tsk (*tpDL_ROS33Table->ros33_tslp_tsk)
#define wup_tsk (*tpDL_ROS33Table->ros33_wup_tsk)
#define can_wup (*tpDL_ROS33Table->ros33_can_wup)

/* Synchronization and Communication Functions */
#define sig_sem (*tpDL_ROS33Table->ros33_sig_sem)


#define twai_sem (*tpDL_ROS33Table->ros33_twai_sem)
#define set_flg (*tpDL_ROS33Table->ros33_set_flg)
#define clr_flg (*tpDL_ROS33Table->ros33_clr_flg)


#define twai_flg (*tpDL_ROS33Table->ros33_twai_flg)

#define snd_msg (*tpDL_ROS33Table->ros33_snd_msg)


#define trcv_msg (*tpDL_ROS33Table->ros33_trcv_msg)

/* Interrupt Management Functions */
#define ret_int (*tpDL_ROS33Table->ros33_ret_int)
#define ent_int (*tpDL_ROS33Table->ros33_ent_int)
#define loc_cpu (*tpDL_ROS33Table->ros33_loc_cpu)
#define unl_cpu (*tpDL_ROS33Table->ros33_unl_cpu)

/* Time Management Functions */
#define set_tim (*tpDL_ROS33Table->ros33_set_tim)
#define get_tim (*tpDL_ROS33Table->ros33_get_tim)
#define dly_tsk (*tpDL_ROS33Table->ros33_dly_tsk)
#define def_cyc (*tpDL_ROS33Table->ros33_def_cyc)
#define act_cyc (*tpDL_ROS33Table->ros33_act_cyc)
#define def_alm (*tpDL_ROS33Table->ros33_def_alm)
#define ret_tmr (*tpDL_ROS33Table->ros33_ret_tmr)

/* System Management Functions */
#define get_ver (*tpDL_ROS33Table->ros33_get_ver)

/* Extended Synchronization and Communication Functions */


#define tsnd_mbf (*tpDL_ROS33Table->ros33_tsnd_mbf)


#define trcv_mbf (*tpDL_ROS33Table->ros33_trcv_mbf)

/* Memory Pool Management Functions */


#define tget_blk (*tpDL_ROS33Table->ros33_tget_blk)
#define rel_blk (*tpDL_ROS33Table->ros33_rel_blk)


#define tget_blf (*tpDL_ROS33Table->ros33_tget_blf)
#define rel_blf (*tpDL_ROS33Table->ros33_rel_blf)


/***************************************************************************
***    6.7 Error Codes                                                   ***
***************************************************************************/
#define E_OK       0        /* Normal completion */
#define E_SYS      (-5)     /* System error */
#define E_NOMEM    (-10)    /* Insufficient memory */
#define E_NOSPT    (-17)    /* Feature not supported */
#define E_INOSPT   (-18)    /* Feature not supported 
                               by ITRON/FILE specification */
#define E_RSFN     (-20)    /* Reserved function code number */
#define E_RSATR    (-24)    /* Reserved attribute */
#define E_PAR      (-33)    /* Parameter error */
#define E_ID       (-35)    /* Invalid ID number */
#define E_NOEXS    (-52)    /* Object does not exist */
#define E_OBJ      (-63)    /* Invalid object state */
#define E_MACV     (-65)    /* Memory access disabled or memory access 
                               violation */
#define E_OACV     (-66)    /* Object access violation */
#define E_CTX      (-69)    /* Context error */
#define E_QOVR     (-73)    /* Queuing or nesting overflow */
#define E_DLT      (-81)    /* Object being waited for was deleted */
#define E_TMOUT    (-85)    /* Polling failure or timeout exceeded */
#define E_RLWAI    (-86)    /* WAIT state was forcibly released */


#endif /* _ITRON_ */

